package com.demoweb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.demoweb.model.dao.BoardDao;
import com.demoweb.model.dto.Board;
import com.demoweb.ui.ThePager;

@WebServlet("/board/list.action")
public class BoardListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int currentPage = 1;
		int pageSize = 2;
		int dataCount = 0;
		int pagerSize = 2;
		String url = "list.action";
		
		//
		String page = request.getParameter("pageno");		
		if (page != null && page.length() > 0) {
			currentPage = Integer.parseInt(page);
		}		
		int startRow = (currentPage - 1) * pageSize;		
		//데이터베이스에서 데이터 조회
		BoardDao dao = new BoardDao();
		List<Board> boards = dao.getBoardList(startRow, pageSize);
		dataCount = dao.getBoardCount();
		
		//조회된 데이터를 jsp 처리할 수 있도록 request 객체에 저장
		request.setAttribute("boards", boards);
		ThePager pager = 
			new ThePager(dataCount, currentPage, pageSize, pagerSize, url);
		request.setAttribute("pager", pager);
		
		RequestDispatcher dispatcher = 
			request.getRequestDispatcher("/WEB-INF/views/board/boardlist.jsp");

		dispatcher.forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
